//
//  CollegeTableViewController.m
//  LoLBox
//
//  Created by lanou3g on 15/5/31.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CollegeTableViewController.h"

@interface CollegeTableViewController ()<MBProgressHUDDelegate>

{
    UIView *view;
}
@property (nonatomic , strong) NSMutableArray * dataArray;

@property (nonatomic , strong) MBProgressHUD * hub;

@end

@implementation CollegeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    [self parseUrl];
    
    [self settings];
    
    [self showUploadJuHua];
}


-(void)showUploadJuHua
{
    view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    view.backgroundColor = [UIColor whiteColor  ];
    [self.view addSubview:view];
    [self.view bringSubviewToFront:view];
    
    self.hub = [[MBProgressHUD alloc]initWithView:view];
    self.hub.mode = MBProgressHUDModeDeterminateHorizontalBar;
    
    self.hub.labelText = @"拼命加载中";
    
    _hub.delegate = self;
    [_hub showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];
    
    [self.tableView addSubview:self.hub];
    
    [self.hub show:YES];
}


- (void)myProgressTask {
    // This just increases the progress indicator in a loop
    float progress = 0.0f;
    while (progress < 1.0f) {
        progress += 0.02f;
        self.hub.progress = progress;
        usleep(50000);
    }
     [view removeFromSuperview];
}



- (void)myTask {
    // Do something usefull in here instead of sleeping ...
    sleep(1);
    [view removeFromSuperview];
}






-(void)settings
{

    self.navigationItem.title = @"英雄时刻";
    
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    self.navigationController.navigationBar.backgroundColor = [UIColor blackColor];
    
    UIBarButtonItem * leftBI = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(leftBIWay)];
    
    self.navigationItem.leftBarButtonItem = leftBI;
    
    UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"niuniu.png"]];
    
     imageView.frame = CGRectMake(0, 0, 375, 675);
    
    self.tableView.backgroundView = imageView;
    
    

}


-(void)leftBIWay
{
    
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
        nil;
    }];
    
}


-(void)parseUrl
{
    self.dataArray = [NSMutableArray array];
    
    NSURL * url = [NSURL URLWithString:@"http://112.124.12.126:8080/LOLWebServers/api/getvideoclasslist?tag=%E8%A7%A3%E8%AF%B4%E8%A7%86%E9%A2%91"];
    
    NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    
    __weak CollegeTableViewController * collegeTVc = self;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        if (data ==nil) {
            return ;
        }
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        for (NSDictionary * dict in dic) {
            
            CollegeModle * modle = [[CollegeModle alloc]init];
            
            [modle setValuesForKeysWithDictionary:dict];
            
            [collegeTVc.dataArray addObject:modle];
            
           // NSLog(@"%@",modle);
        }
        [collegeTVc.tableView reloadData];
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return _dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CollegeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"colcell" ];
    
    if (!cell) {
        
        cell = [[CollegeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"colcell"];
    }
    
    //cell设置透明  必须的3项
   // cell.opaque = NO;
    
    cell.backgroundColor = [UIColor clearColor];
    
   // cell.backgroundColor = [UIColor colorWithRed:(250.0/255.0) green:(255.0/255.0) blue:(255.0/255.0) alpha:1.0];
    
    cell.selectedBackgroundView =  [[UIImageView alloc]init];
    
    cell.selectedBackgroundView.backgroundColor = [UIColor purpleColor];

    
    CollegeModle * modle = _dataArray[indexPath.row];
    
    //NSLog(@"%@",modle);
    
    cell.modle = modle;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];

    
    CollegeModle * modle = _dataArray[indexPath.row];

    MovieTableViewController * movieTVC = [[MovieTableViewController alloc]init];
    
    //中文转码
    NSString * new = [modle.flmc stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    movieTVC.str = new;
    
    [self.navigationController pushViewController:movieTVC animated:YES];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;

}





/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
